package com.br.apurn.controller;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import com.br.apurn.model.Usuario;

@ManagedBean
@SessionScoped
public class UserMBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Usuario user;
	
	
	public UserMBean() {
		user = new Usuario();
		SecurityContext context = SecurityContextHolder.getContext();
		if (context instanceof SecurityContext) {
			Authentication authentication = context.getAuthentication();
			if (authentication instanceof Authentication) {
				user.setName(((org.springframework.security.core.userdetails.User)authentication.getPrincipal()).getUsername());
			}
		}
	}
	

	public Usuario getUser() {
		return user;
	}

	public void setUser(Usuario user) {
		this.user = user;
	}
	
}
